package pe.edu.upc.dew.grupo3a.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.support.WebApplicationContextUtils;

import pe.edu.upc.dew.grupo3a.model.Pelicula;
import pe.edu.upc.dew.grupo3a.service.PeliculaService;

/**
 * Servlet implementation class CarteleraServLet
 */
public class CarteleraServLet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PeliculaService peliculaService;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CarteleraServLet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
	@Override
	public void init(ServletConfig config) throws ServletException	{
			this.peliculaService = WebApplicationContextUtils
			.getWebApplicationContext(config.getServletContext())
			.getBean("peliculaService", PeliculaService.class);
			
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String nombre = request.getParameter("nombre");
		String genero = request.getParameter("genero");
		String clasificacion = request.getParameter("cmbclasifiacion");
		//
		List<Pelicula> peliculas = new ArrayList<Pelicula>(); 
		if(nombre.length()>0){
			Pelicula pelicula = peliculaService.getPeliculaPorNombre(nombre);
			peliculas.add(pelicula);
		}
		if(genero.length()>0){
		   peliculas =  peliculaService.getPeliculasPorGenero(genero);
		}
		if(clasificacion.length()>0 && !clasificacion.equals("none")){
			peliculas = peliculaService.getPeliculasPorClasificacion(clasificacion);
		}
		List<Pelicula> peliculasEstreno = peliculaService.getPeliculasPorEstado("Estreno");
		List<Pelicula> peliculasPorEstrenar = peliculaService.getPeliculasPorEstado("Proximamente");
		//
		request.setAttribute("peliculasEstreno", peliculasEstreno);
		request.setAttribute("peliculasPorEstrenar", peliculasPorEstrenar);
		//
		request.setAttribute("peliculas", peliculas);
		request.getRequestDispatcher("cartelera.jsp").forward(request,response);
	}

}
